import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/auth-redirect',
    component: () => import('@/views/login/auth-redirect'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true },

  {
    path: '/',
    component: Layout,
    redirect: '/home',
    children: [
      {
        path: 'home',
        component: () => import('@/views/Home/index'),
        name: 'Home',
        meta: { title: '首页', icon: 'documentation', affix: true }
      }
    ]
  },
  {
    path: '/brand',
    component: Layout,
    name: 'Brand',
    meta: { title: '品牌管理', icon: 'documentation' },
    alwaysShow: true,
    children: [
      {
        path: 'index',
        component: () => import('@/views/BrandManagement/index.vue'),
        name: 'BrandManagement',
        meta: { title: '品牌信息', icon: 'documentation' }
      }
    ]
  },
  {
    path: '/floor',
    component: Layout,
    name: 'ShopFloor',
    meta: { title: '车间管理', icon: 'documentation' },
    alwaysShow: true,
    children: [
      {
        path: 'index',
        component: () => import('@/views/ShopFloor/index.vue'),
        name: 'ShopFloorManage',
        meta: { title: '车间信息', icon: 'documentation' }
      }
    ]
  },
  {
    path: '/protocol',
    component: Layout,
    name: 'Protocol',
    meta: { title: '协议管理', icon: 'documentation' },
    alwaysShow: true,
    children: [
      {
        path: 'index',
        component: () => import('@/views/Protocol/index.vue'),
        name: 'ProtocolManage',
        meta: { title: '平台协议管理', icon: 'documentation' }
      }
    ]
  },
  {
    path: '/coupon',
    component: Layout,
    name: 'Coupon',
    meta: { title: '优惠卷管理', icon: 'documentation' },
    alwaysShow: true,
    children: [
      {
        path: 'index',
        component: () => import('@/views/Coupon/index.vue'),
        name: 'CouponManage',
        meta: { title: '优惠卷信息', icon: 'documentation' }
      }
    ]
  },
  {
    path: '/user',
    component: Layout,
    name: 'User',
    meta: { title: '用户管理', icon: 'documentation' },
    alwaysShow: true,
    children: [
      {
        path: 'index',
        component: () => import('@/views/User/index.vue'),
        name: 'UserManage',
        meta: { title: '用户信息', icon: 'documentation' }
      }
    ]
  },
  {
    path: '/personnel',
    component: Layout,
    name: 'Personnel',
    meta: { title: '人员管理', icon: 'documentation' },
    alwaysShow: true,
    children: [
      {
        path: 'customer',
        component: () => import('@/views/User/index.vue'),
        name: 'Customer',
        meta: { title: '平台客服信息', icon: 'documentation' }
      },
      {
        path: 'mechanic',
        component: () => import('@/views/User/index.vue'),
        name: 'Mechanic',
        meta: { title: '维修技工信息', icon: 'documentation' }
      }
    ]
  },
  {
    path: '/chat',
    component: Layout,
    name: 'GroupChat',
    meta: { title: '群聊管理', icon: 'documentation' },
    alwaysShow: true,
    children: [
      {
        path: 'index',
        component: () => import('@/views/GroupChat/index.vue'),
        name: 'GroupChatManage',
        meta: { title: '群聊信息', icon: 'documentation' }
      }
    ]
  },
  {
    path: '/garage',
    component: Layout,
    name: 'Garage',
    meta: { title: '车库管理', icon: 'documentation' },
    alwaysShow: true,
    children: [
      {
        path: 'index',
        component: () => import('@/views/Garage/index.vue'),
        name: 'GarageManage',
        meta: { title: '车库信息', icon: 'documentation' }
      }
    ]
  },
  {
    path: '/order',
    component: Layout,
    name: 'Order',
    meta: { title: '订单管理', icon: 'documentation' },
    alwaysShow: true,
    children: [
      {
        path: 'evaluation',
        component: () => import('@/views/Order/Evaluation/index.vue'),
        name: 'GarageManage',
        meta: { title: '订单评价', icon: 'documentation' }
      },
      {
        path: 'equip',
        component: () => import('@/views/Order/Equipment/index.vue'),
        name: 'OrderEquip',
        meta: { title: '设备订单', icon: 'documentation' }
      },
      {
        path: 'consulting',
        component: () => import('@/views/Order/Consulting/index.vue'),
        name: 'Consulting',
        meta: { title: '咨询订单', icon: 'documentation' }
      },
      {
        path: 'repair',
        component: () => import('@/views/Order/Repair/index.vue'),
        name: 'Repair',
        meta: { title: '维修工单', icon: 'documentation' }
      }
    ]
  },
  {
    path: '/dashboard',
    component: Layout,
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        name: 'Dashboard',
        meta: { title: 'Dashboard', icon: 'dashboard' }
      }
    ]
  },
  {
    path: '/profile',
    component: Layout,
    redirect: '/profile/index',
    hidden: true,
    children: [
      {
        path: 'index',
        component: () => import('@/views/profile/index'),
        name: 'Profile',
        meta: { title: 'Profile', icon: 'user', noCache: true }
      }
    ]
  }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
